package service;

import dao.ProductDao;
import dao.ShopDao;
import dao.StoreDao;
import po.*;

public class SellerService {
	public static boolean purchase(String productId, String sellerId, int amount) { // 进货
		// 判断该商品自己以前是否已经进货过
		if (amount <= 0) return false;
		Store s = new Store();
		s.setProductId(productId);
		s.setSellerId(sellerId);
		if (StoreDao.checkRecordExistence(s)) { // 进货过
			// 需要更新数量
			StoreDao.queryOneRecord(s);
			s.setAmount(s.getAmount() + amount);
			StoreDao.updataOneRecord(s);
		} else { // 未进货过
			s.setAmount(amount);
			StoreDao.addOneRecord(s);
		}
		return true;
	}
	
	public static boolean delivery(String productId, String sellerId, String buyerId) { // 发货
		Shop s = new Shop();
		s.setProductId(productId);
		s.setSellerId(sellerId);
		s.setBuyerId(buyerId);
		// 写数据前需要补齐其他数据
		ShopDao.queryOneRecord(s);
		s.setStatus(2); // 2 表示交易成功
		// 写回数据
		ShopDao.updataOneRecord(s);
		return true;
	}
	
	public static Shop[] getAllOrder(String sellerId) {
		Shop[] allShop = ShopDao.queryAllRecord();
		Shop[] myOrder;
		int amount=0;
		for (Shop temp: allShop) {
			if (temp.getSellerId().equals(sellerId) && temp.getStatus() == 1)
				amount++;
		}
		myOrder = new Shop[amount];
		amount=0;
		for (Shop temp: allShop) {
			if (temp.getSellerId().equals(sellerId) && temp.getStatus() == 1)
				myOrder[amount++] = temp;
		}
		return myOrder;
	}
	
	
	public static Product[] getAllProduct() {
		return ProductDao.queryAllRecord();
	}
	
}
